import { ref } from "vue";
import regions from './regions.json';
export const address = ref(null);
const provinces = ref([]);
const cities = ref([]);
const areas = ref([]);
const pickerValue = ref([0, 0, 0]);
export const addressList = ref([provinces.value, cities.value, areas.value]);
export const addressFn = () => {
    getAddress();
    address.value.open();
};
export const getAddress = () => {
    const sortedProvinces = regions.sort((a, b) => a.code.localeCompare(b.code));
    provinces.value = sortedProvinces.map(province => ({
        name: province.name,
        children: province.children.sort((a, b) => a.code.localeCompare(b.code))
    }));

    // 设置默认选择
    pickerValue.value[0] = 0;
    cities.value = provinces.value[pickerValue.value[0]].children;
    areas.value = cities.value[0]?.children || [];
    addressList.value = [provinces.value, cities.value, areas.value];

    if (address.value) {
        address.value.setIndexs([pickerValue.value[0], 0, 0], true);
    }
    // console.log(addressList.value, '地址-----')
};
export const change = (e) => {
    const { columnIndex, index } = e;

    if (columnIndex === 0) {
        cities.value = provinces.value[index]?.children || [];
        areas.value = cities.value[0]?.children || [];
        addressList.value = [provinces.value, cities.value, areas.value];
        address.value.setIndexs([index, 0, 0], true);
    } else if (columnIndex === 1) {
        areas.value = cities.value[index]?.children || [];
        addressList.value = [provinces.value, cities.value, areas.value];
        address.value.setIndexs([e.indexes[0], index, 0], true);
    }
};